import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/commonwidget/common_base_widget.dart';

class TextFieldWidget extends BaseStatefulWidget {
  final String? initText;

  final double width;
  final double? height;

  final double paddingLeft;
  final double paddingRight;
  final double paddingTop;
  final double paddingBottom;

  final int maxLine;
  final int? maxLength;
  final bool isEnable;

  final double radius;

  final TextAlign textAlign;

  final Color insideColor;
  final Color cursorColor;
  final Color textColor;

  final bool enableCountText;

  final TextInputType keyboardType;
  final OnTextChange? onTextChange;
  final OnFocus? onFocus;

  final bool isPassword;
  final double fontSize;

  final List<TextInputFormatter>? formatterList;

  const TextFieldWidget({
    Key? key,
    this.initText,
    this.width = 100,
    this.height,
    this.paddingBottom = 0,
    this.paddingLeft = 15,
    this.paddingRight = 15,
    this.paddingTop = 0,
    this.maxLine = 1,
    this.maxLength, // 需要和enableCountText搭配使用，否则不显示效果
    this.textAlign = TextAlign.left,
    this.isEnable = true,
    this.radius = 20,
    this.fontSize = 18,
    this.insideColor = const Color(0xFF529FE8),
    this.cursorColor = Colors.white,
    this.textColor = Colors.white,
    this.enableCountText = false, // 需要和maxLength搭配使用，否则不显示效果
    this.keyboardType = TextInputType.text,
    this.onTextChange,
    this.isPassword = false,
    this.onFocus,
    this.formatterList,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TextFieldWidgetState();
  }
}

class _TextFieldWidgetState extends BaseWidgetState<TextFieldWidget> {
  late TextEditingController _controller;
  late FocusNode _focusNode;
  String lastText = '';

  @override
  void initState() {
    _controller = TextEditingController(text: widget.initText);
    _focusNode = FocusNode();
    _controller.addListener(() {
      if (lastText != _controller.text) {
        // text change
        if (widget.onTextChange != null) {
          widget.onTextChange!(_controller.text);
        }
      }
      lastText = _controller.text;
    });
    // 聚焦
    _focusNode.addListener(() {
      if (widget.onFocus != null) {
        widget.onFocus!(_focusNode.hasFocus);
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      height: widget.height,
      decoration: BoxDecoration(
        color: widget.insideColor,
        borderRadius: BorderRadius.circular(widget.radius), // 圆角度
      ),
      padding: EdgeInsets.only(
        left: widget.paddingLeft,
        right: widget.paddingRight,
        top: widget.paddingTop,
        bottom: widget.paddingBottom,
      ),
      child: Center(
        child: TextField(
          focusNode: _focusNode,
          cursorColor: widget.cursorColor,
          maxLength: widget.maxLength,
          maxLines: widget.maxLine,
          textAlign: widget.textAlign,
          controller: _controller,
          keyboardType: widget.keyboardType,
          enabled: widget.isEnable,
          style: TextStyle(color: widget.textColor, fontSize: widget.fontSize),
          obscureText: widget.isPassword,
          inputFormatters: widget.formatterList,
          decoration: InputDecoration(
            border: InputBorder.none,
            counterText: widget.enableCountText ? null : '',
          ),
        ),
      ),
    );
  }
}
